package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.dto.StoreGoodsDiscountDto;
import com.dingreading.cloud.admin.entity.StoreGoodsDiscount;
import com.dingreading.cloud.admin.entity.Stores;
import com.dingreading.cloud.admin.service.StoreGoodsDiscountService;
import com.dingreading.cloud.admin.service.StoresService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/system/storeGoodsDiscount")
@Api(tags = "轻量门店的商品折扣管理")
public class StoreGoodsDiscountController {

    @Resource
    private StoreGoodsDiscountService storeGoodsDiscountService;
    @Resource
    private StoresService storesService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "门店名称") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        Page<StoreGoodsDiscountDto> pageList = storeGoodsDiscountService.pageList(pageUtil, keyword, enabled);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(pageList.getTotalRow());
        data.setList(pageList.getRecords());
        return R.ok(data);
    }

    @ApiOperation(value = "添加/编辑")
    @PostMapping(value = "/add", produces = "application/json; charset=UTF-8")
    public R<Object> add(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "折扣率") @RequestParam(value = "discountRate") Double discountRate
    ) {
        if (discountRate == null || discountRate <= 0)
            return R.fail("请填写折扣率");

        Stores stores = storesService.getByUid(storeUid);
        if (stores == null) {
            return R.fail("未查询到门店");
        }
        if (StringUtils.isBlank(stores.getSubjectType())) {
            return R.fail("门店未设置科目类型");
        }
        // 新增
        StoreGoodsDiscount data = storeGoodsDiscountService.getByStoreUid(storeUid);
        if (data == null) {
            data = new StoreGoodsDiscount();
            data.setAddStaffUid(headStaffUid);
            data.setAddStaffName(headStaffName);
            data.setLastStaffUid(headStaffUid);
            data.setLastStaffName(headStaffName);
            data.setStoreUid(storeUid);
            data.setDiscountRate(discountRate);
            data.setEnabled(1);
            storeGoodsDiscountService.saveOrUpdate(data);
        } else {
            // 修改
            if (!data.getEnabled().equals(1)) {
                return R.fail("该折扣已被禁用，不能操作");
            }
            boolean b = storeGoodsDiscountService.updateInfo(storeUid, discountRate);
            if (!b)
                return R.fail();
        }
        return R.ok();
    }

    @ApiOperation(value = "获取详情")
    @PostMapping(value = "/get", produces = "application/json; charset=UTF-8")
    public R<Object> get(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid
    ) {
        StoreGoodsDiscountDto data = storeGoodsDiscountService.dtoGetByStoreUid(storeUid);
        if (data == null) {
            Stores stores = storesService.getByUid(storeUid);
            if (stores == null) {
                return R.fail("未查询到门店");
            }
            data = new StoreGoodsDiscountDto();
            data.setStoreUid(storeUid);
            data.setSubjectType(stores.getSubjectType());
            data.setStoreName(stores.getStoreName());
        }
        return R.ok(data);
    }

    @ApiOperation(value = "修改启用状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "启用状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        boolean l = storeGoodsDiscountService.updateEnabled(storeUid, enabled);
        if (!l)
            return R.fail("修改失败");
        return R.ok();
    }

}
